package com.self.serviceedu.controller;


import com.self.commonutils.R;
import com.self.serviceedu.client.VodClient;
import com.self.serviceedu.entity.EduVideo;
import com.self.serviceedu.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author xiaozeng
 * @since 2020-09-16
 */
@Api(description = "课程小节管理")
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    //注入vodClient
    @Autowired
    private VodClient vodClient;

    //添加小节
    @ApiOperation(value = "添加小节")
    @PostMapping("addVideo")
    public R addVideo(
            @ApiParam(name = "eduVideo",value = "课程小节",required = true)
            @RequestBody EduVideo eduVideo){

        videoService.save(eduVideo);
        return R.ok();
    }


    //删除小节
    // TODO 后面这个方法是需要完善的: 在删除小节时，需要将视频一起删除

    @ApiOperation(value = "删除小节")
    @DeleteMapping("{id}")
    public R deleteVideo(
            @ApiParam(name = "id",value = "小节Id",required = true)
            @PathVariable("id") String id) {

        //根据小节Id获取视频Id，然后删除视频。
        EduVideo video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            vodClient.removeVideo(videoSourceId);
        }

        videoService.removeById(id);
        return R.ok();
    }

    //修改小节 TODO


}

